local skel = fk.CreateSkill {
  name = "emo__yuchen",
}

Fk:loadTranslationTable{
  ["emo__yuchen"] = "驭臣",
  [":emo__yuchen"] = "准备阶段，若场上没有以此法放置的装备牌，你重复X次（X为本局角色数的一半，向上取整）：检索一张装备牌，置入一名角色的装备区并替换原装备，然后令其可以使用一张检索过程亮出的牌。",

  ["@$emo__yuchen"] = "驭臣",
  ["#emo__yuchen-put"] = "驭臣：将 %arg 置入一名角色装备区",
  ["#emo__yuchen-use"] = "驭臣：你可以使用其中一张牌",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and data.phase == Player.Start then
      local mark = player:getTableMark("@$emo__yuchen")
      return not table.find(mark, function (id) return player.room:getCardArea(id) == Card.PlayerEquip end)
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local limit = 999
    local num = math.ceil(#room.players / 2)
    local equips = {}
    for _ = 1, num do
      if player.dead then break end
      local cards = {}
      for i = 1, limit do
        if player.dead then break end
        local cid = room:getNCards(1)[1]
        room:moveCardTo(cid, Card.Processing, nil, fk.ReasonPut, skel.name, nil, true, player)
        if player.dead then break end
        room:delay(i < 6 and 400 or 100)
        table.insert(cards, cid)
        local card = Fk:getCardById(cid)
        if card.type == Card.TypeEquip then
          table.insert(equips, cid)
          local targets = table.filter(room.alive_players, function (p) return p:canMoveCardIntoEquip(cid, true) end)
          if #targets > 0 then
            local to = room:askToChoosePlayers(player, {
              min_num = 1, max_num = 1, skill_name = skel.name, targets = targets, cancelable = false,
              prompt = "#emo__yuchen-put:::".. card:toLogString()
            })[1]
            room:moveCardIntoEquip(to, cid, skel.name, true, player)
            cards = table.filter(cards, function (id) return room:getCardArea(id) == Card.Processing end)
            if to:isAlive() and #cards > 0 then
              room:askToUseRealCard(to, {
                pattern = cards, cancelable = true, expand_pile = cards, skill_name = skel.name,
                prompt = "#emo__yuchen-use",
              })
            end
          end
          break
        end
      end
      room:cleanProcessingArea(cards, skel.name)
    end
    if not player.dead then
      equips = table.filter(equips, function (id) return room:getCardArea(id) == Card.PlayerEquip end)
      room:setPlayerMark(player, "@$emo__yuchen", #equips > 0 and equips or 0)
    end
  end,
})

-- 每阶段开始更新一次吧
skel:addEffect(fk.EventPhaseStart, {
  can_refresh = function(self, event, target, player, data)
    return player:getMark("@$emo__yuchen") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local mark = table.filter(player:getTableMark("@$emo__yuchen"), function (id) return room:getCardArea(id) == Card.PlayerEquip end)
    room:setPlayerMark(player, "@$emo__yuchen", #mark > 0 and mark or 0)
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@$emo__yuchen", 0)
end)

return skel
